<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
use think\facade\Log;

/**
 * 网络速度接口
 */
class Networkspeed extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [];
    /**
     * 默认响应输出类型,支持json/xml
     * @var string 
     */
    // protected $responseType = '';

    public function data()
    {
        $server_num = $this->request->post('servernum');
        if(!$server_num){
            $server_num = config('network_speed')['server_num'];
        }
        $type_num = $this->request->post('typenum');
        if(!$type_num){
            $type_num = config('network_speed')['type_num'];
        }
        $page_num = $this->request->post('pagenum');
        if(!$page_num){
            $page_num = config('network_speed')['page_num'];
        }
        $server = Db::name('isp')
            ->field('id,name,content,img_id as imgId,files')
            ->order('sort desc')
            ->limit($server_num)
            ->select();  
        foreach ($server as $key => &$s_v) {
            $s_v['files'] = explode(',', $s_v['files']);
            $server[$key] = $s_v;
        }
        $arr = Db::name('page')
            ->alias('p')
            ->field('pt.type,pt.name as pt_name,pt.id as pt_id,pt.icon,p.name,p.id,p.url')
            ->join('page_type pt','p.page_type_id=pt.id','left')
            ->order('pt.sort desc,p.sort desc')
            ->select();
        $arr2 = array();
        foreach ($arr as $key => $value) {
            $arr2[$value['pt_id']][] = $value;         
        }
        // dump($arr2);exit;
        $webpages = array();
        $i=0;
        foreach ($arr2 as $key => $value) {
            $item = array();
            if($i++ > ($type_num-1) ){
                break;
            }
            foreach ($value as $key2 => $value2) {
                if($key2 > ($page_num-1) ){
                    break;
                }
                $item['id'] = $value2['pt_id'];
                $item['type'] = $value2['type'];
                $item['name'] = $value2['pt_name'];
                if(isset($value2['icon']) && $value2['icon']){
                    $item['icon'] = request()->domain().$value2['icon'];
                }else{
                    $item['icon'] = '';
                }
                unset($value2['pt_id']);
                unset($value2['type']);
                unset($value2['pt_name']);
                unset($value2['icon']);
                $item['list'][] = $value2;
            }
            $webpages[] = $item;
        }
        $data_arr = array(
            'server'=>$server,
            'webpages'=>$webpages
        );
        $this->success('', $data_arr);
    }

}
